package com.hmy.cornpestdet.service;

import com.hmy.cornpestdet.mapper.UserDataAnalyzeMapper;
import com.hmy.cornpestdet.utils.CPDConst;
import com.hmy.cornpestdet.utils.MonthRiceDataListToObj;
import com.hmy.cornpestdet.utils.Result;
import com.hmy.cornpestdet.vo.MonthRiceDataVo;
import com.hmy.cornpestdet.vo.RiceDataVo;
import com.hmy.cornpestdet.vo.UserUploadCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserDataAnalyzeService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    UserDataAnalyzeMapper userDataAnalyzeMapper;

    public Result getAllRiceDataByUid(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);

        List<RiceDataVo>list =  userDataAnalyzeMapper.getAllRiceDataByUid(uid);
        if(list == null){
            return Result.Error("获取失败");
        }
        return Result.Success("获取成功",list);

    }

    public Result getSingleMonthRiceDataByUid( int month){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        List<RiceDataVo>list =  userDataAnalyzeMapper.getSingleMonthRiceDataByUid(month,uid);
        if(list == null){
            return Result.Error("获取失败");
        }
        return Result.Success("获取成功",list);
    }


    public Result getAllUploadCountByUid(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        List<UserUploadCountVo>list =  userDataAnalyzeMapper.getAllUploadCountByUid(uid);
        if(list == null){
            return Result.Error("获取失败");
        }
        Map<Integer,Integer>map = new HashMap<>();
        for(UserUploadCountVo vo : list){
            map.put(vo.getMonth(),vo.getCount());
        }
        List<Integer>returnList = new ArrayList<>();
        for(int i=1;i<=12;i++){
            if(map.containsKey(i)){
                returnList.add(map.get(i));
            }
            else{
                returnList.add(0);
            }
        }
        return Result.Success("获取成功",returnList);

    }

    public Result getAllMonthRiceDataByUid(){
        //获取当前的uid
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        List<MonthRiceDataVo>list =  userDataAnalyzeMapper.getAllMonthRiceDataByUid(uid);
        if(list == null){
            return Result.Error("获取失败");
        }

        return Result.Success("获取成功", MonthRiceDataListToObj.work(list));
    }

}
